from django.shortcuts import render

from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView
from .models import Picture
from django.shortcuts import get_object_or_404
from .forms import Upoad
from django.shortcuts import redirect
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger

# Create your views here.

def PicList(request):
    pic_list = Picture.objects.all().order_by('-date')
    paginator = Paginator(pic_list, 10)

    page = request.GET.get('page')
    picture = paginator.get_page(page)
    return render(request, 'pic_upload/picture_list.html', {'pic_list': picture})

def Pic_Detail(request):
    pic_detail = get_object_or_404(Picture, pk=id)
    return render(request, 'pic_upload/picture_detail.html', {'pic_detail': pic_detail})

def Pic_Creat(request):
    if request == 'POST':
        pic_creat = Upoad()
        pic_title = pic_creat.title
        pic_image = pic_creat.image
        pic_date = pic_creat.data
        pic_creat.save()
        return render(request, 'pic_upload/picture_form.html', {pic_creat: 'pic_creat'})
    else:
        message = '请正确上传'
        return redirect('picture_list')





















# class PicList(ListView):
#     queryset = Picture.objects.all().order_by('-date')
#     context_object_name = 'latest_picture_list'

# class PicDetail(DetailView):
#     model = Picture
#     # DetailView默认Context_object_name是picture
#
#     # 下面是DetailView默认模板，可以换成自己的
#     # template_name = 'pic_upload/picture_detail.html'
#
# class PicUpload(CreateView):
#     model = Picture
#     fields = ['title', 'image']
